package com.climber.spb_rabbitmq.base;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class BaseConsumer {
    public static void main(String[] args) {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("127.0.0.1");
        factory.setUsername("guest");
        factory.setPassword("guest");

        try {
            Connection connection = factory.newConnection();
            Channel channel = connection.createChannel();
            System.out.println("等待接收消息.........");
            //推送的消息如何进行消费的接口回调
            // DeliverCallback 是函数式接口
            DeliverCallback deliverCallback =
                    (consumerTag, delivery) -> {
                        String message = new String(delivery.getBody());
                        System.out.println(message);
                    };
            //取消消费的一个回调接口 如在消费的时候队列被删除掉了
            CancelCallback cancelCallback =
                    (consumerTag) -> {
                        System.out.println("消息消费被中断");
                    };

            channel.basicConsume("Q0101",true,deliverCallback,cancelCallback);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        }


    }
}
